Accessibility

DRK Volume 4 Article

Form Filter Tag

What Is the Form Filter Tag?

The Form Filter tag helps ColdFusion developers process data submitted either through POST or GET methods. It automates the common practice of trimming URL or FORM variables, simplifies the process of checking for their existence, and consolidates all URL and FORM variables into a single scope so that the code processing the request does not need to distinguish between data submitted via a POST or a GET.

The Form Filter tag acts as a "filter." This means you can just use the tag on your page and it will automatically filter and alter the FORM, URL, and request scopes based on its configuration. Your code assumes that at any point after the tag has been used, the requested changes have been successfully made.

Installing the Form Filter Tag

You can install and reference the Form Filter tag just like any other custom tag. If it is in the same directory as your ColdFusion page, simply use it like this:

<cf_formfilter ... />

You can also store your custom tags in a directory specified in the ColdFusion Administrator under Extensions > Custom Tag Paths. Additionally, you can use the cfimport tag to import a directory of tags and then reference them with your own prefix, like this:

<!---
	This example assumes that formfilter.cfm is in a directory called 
	cf_tags directly off the web root.
--->
<cfimport taglib="/cf_tags" prefix="mm" />
<mm:formfilter ... >

Using the Form Filter Tag

The Form Filter tag helps ColdFusion developers process form data in three ways:

  • The Form Filter can put all FORM and URL variables into the request scope so that your code does not have to distinguish between data submitted via POST or GET. Rather than referencing variables with either the "url" or "form" scope prefixes, you can unconditionally reference all variables using just the "request" scope.
  • The Form Filter can automatically trim all the data submitted to the server, which is typically considered good practice. It is easy to introduce white space into HTML forms, which can cause problems later when trying to compare strings. For instance, if a username or password gets saved in the database with extra spaces before or after it, it could prevent the user from being able to be authenticated in the future.
  • Some HTML form inputs get submitted as empty strings when users leave them blank. In order to confirm their existence, you usually have to check if they are defined in the first place and see if they are empty strings or not. The Form Filter tag can automatically remove zero-length variables so that your code only needs to check whether the variable is defined.

To make the Form Filter tag do one or more of the functions above, simply use the tag in the page or component that processes the request. The Form Filter tag has a single attribute, called "action," that determines which services it provides. The four possible values are described in the table below:

Table 1. Possible attributes of the Form Filter tag


Attribute Description
request Puts all URL and FORM variables in the request scope, which means you can access the request scope without having to worry about whether the data was submitted through a form or through a URL. This is nice because sometimes the code processing the request has to handle requests for POSTs and GETs.
trim Trims all URL and FORM variables (both left and right). Trimming variables is generally a good (but tedious) practice, so the Form Filter tag automates the process.
removeBlank Removes empty variables so you can use the isDefined function to check for the existence of variables without also having to check to see if they are empty. An empty variable is either zero length or consists of all white space.
all Performs all the actions listed above. This is the default, so if no action is specified, all the filters will be executed.

How the Form Filter Tag Works

Because custom tags have full access to the FORM, URL, and request scopes, the Form Filter tag is easily able to iterate through and operate on all three scopes. The Form Filter tag makes heavy use of cfscript and "for in" loops in order to be able to affect every variable in a particular scope. For instance, the code that trims all the variables in the form scope looks like this:

<cfscript>
    for (field in form)
    {   
        form[field] = trim(form[field]);
    }
</cfscript>

The Form Filter tag is designed to automate much of the tedious work that goes into processing not just forms but requests in general. Because it is implemented as a custom tag, it's easy to incorporate it into your ColdFusion pages or components; you can also configure it quickly. Once you have added the Form Filter tag to the appropriate places in your application, you can even add functionality to the tag if you need to. For instance, you could easily change the way all forms and requests in your entire application are processed just by editing a single file.